Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
localforage
Advanced tools
The localForage npm package is a fast and simple storage library for JavaScript. It improves the offline experience of web apps by using asynchronous storage (IndexedDB or WebSQL) with a simple, localStorage-like API. localForage uses a driver system that supports IndexedDB, WebSQL, and localStorage.
Storing Data
This feature allows you to store data in the browser's asynchronous storage. The code sample demonstrates setting and then getting an item.
localforage.setItem('key', 'value').then(function () { return localforage.getItem('key'); }).then(function (value) { console.log(value); }).catch(function (err) { console.error(err); });
Retrieving Data
This feature allows you to retrieve data from the browser's asynchronous storage. The code sample demonstrates getting an item.
localforage.getItem('key').then(function (value) { console.log(value); }).catch(function (err) { console.error(err); });
Removing Data
This feature allows you to remove data from the browser's asynchronous storage. The code sample demonstrates removing an item.
localforage.removeItem('key').then(function () { console.log('Key is removed'); }).catch(function (err) { console.error(err); });
Clearing Storage
This feature allows you to clear all data from the browser's asynchronous storage. The code sample demonstrates clearing the storage.
localforage.clear().then(function () { console.log('Storage is now empty'); }).catch(function (err) { console.error(err); });
Iterating Over Keys
This feature allows you to iterate over all key-value pairs in the browser's asynchronous storage. The code sample demonstrates iterating over keys.
localforage.iterate(function (value, key, iterationNumber) { console.log([key, value]); }).then(function () { console.log('Iteration has completed'); }).catch(function (err) { console.error(err); });
Dexie is a wrapper library for indexedDB, which is a low-level API for client-side storage of significant amounts of structured data. Dexie provides a more powerful query language than localForage and is designed to work with complex data structures.
PouchDB is an open-source JavaScript database inspired by Apache CouchDB that is designed to run well within the browser. PouchDB was created to help web developers build applications that work as well offline as they do online. It is more feature-rich than localForage, with synchronization capabilities.
idb-keyval is a super-simple-small promise-based keyval store implemented with IndexedDB, providing a localStorage-like API. It is much smaller in size than localForage but does not have the same level of browser support or features.
localForage is a fast and simple storage library for JavaScript. localForage
improves the offline experience of your web app by using asynchronous storage
(IndexedDB or WebSQL) with a simple, localStorage
-like API.
localForage uses localStorage in browsers with no IndexedDB or WebSQL support. See the wiki for detailed compatibility info.
To use localForage, just drop a single JavaScript file into your page:
<script src="localforage/dist/localforage.js"></script>
<script>localforage.getItem('something', myCallback);</script>
Try the live example.
Download the latest localForage from GitHub, or install with npm:
npm install localforage
Lost? Need help? Try the localForage API documentation. localForage API文档也有中文版。
If you're having trouble using the library, running the tests, or want to contribute to localForage, please look through the existing issues for your problem first before creating a new one. If you still need help, feel free to file an issue.
Because localForage uses async storage, it has an async API. It's otherwise exactly the same as the localStorage API.
localForage has a dual API that allows you to either use Node-style callbacks or Promises. If you are unsure which one is right for you, it's recommended to use Promises.
Here's an example of the Node-style callback form:
localforage.setItem('key', 'value', function (err) {
// if err is non-null, we got an error
localforage.getItem('key', function (err, value) {
// if err is non-null, we got an error. otherwise, value is the value
});
});
And the Promise form:
localforage.setItem('key', 'value').then(function () {
return localforage.getItem('key');
}).then(function (value) {
// we got our value
}).catch(function (err) {
// we got an error
});
Or, use async
/await
:
try {
const value = await localforage.getItem('somekey');
// This code runs once the value has been loaded
// from the offline store.
console.log(value);
} catch (err) {
// This code runs if there were any errors.
console.log(err);
}
For more examples, please visit the API docs.
You can store any type in localForage; you aren't limited to strings like in
localStorage. Even if localStorage is your storage backend, localForage
automatically does JSON.parse()
and JSON.stringify()
when getting/setting
values.
localForage supports storing all native JS objects that can be serialized to JSON, as well as ArrayBuffers, Blobs, and TypedArrays. Check the API docs for a full list of types supported by localForage.
All types are supported in every storage backend, though storage limits in localStorage make storing many large Blobs impossible.
You can set database information with the config()
method.
Available options are driver
, name
, storeName
, version
, size
, and
description
.
Example:
localforage.config({
driver : localforage.WEBSQL, // Force WebSQL; same as using setDriver()
name : 'myApp',
version : 1.0,
size : 4980736, // Size of database, in bytes. WebSQL-only for now.
storeName : 'keyvaluepairs', // Should be alphanumeric, with underscores.
description : 'some description'
});
Note: you must call config()
before you interact with your data. This
means calling config()
before using getItem()
, setItem()
, removeItem()
,
clear()
, key()
, keys()
or length()
.
You can create multiple instances of localForage that point to different stores
using createInstance
. All the configuration options used by
config
are supported.
var store = localforage.createInstance({
name: "nameHere"
});
var otherStore = localforage.createInstance({
name: "otherName"
});
// Setting the key on one of these doesn't affect the other.
store.setItem("key", "value");
otherStore.setItem("key", "value2");
You can use localForage with RequireJS:
define(['localforage'], function(localforage) {
// As a callback:
localforage.setItem('mykey', 'myvalue', console.log);
// With a Promise:
localforage.setItem('mykey', 'myvalue').then(console.log);
});
If you have the allowSyntheticDefaultImports
compiler option set to true
in your tsconfig.json (supported in TypeScript v1.8+), you should use:
import localForage from "localforage";
Otherwise you should use one of the following:
import * as localForage from "localforage";
// or, in case that the typescript version that you are using
// doesn't support ES6 style imports for UMD modules like localForage
import localForage = require("localforage");
If you use a framework listed, there's a localForage storage driver for the models in your framework so you can store data offline with localForage. We have drivers for the following frameworks:
If you have a driver you'd like listed, please open an issue to have it added to this list.
You can create your own driver if you want; see the
defineDriver
API docs.
There is a list of custom drivers on the wiki.
You'll need node/npm and bower.
To work on localForage, you should start by
forking it and installing its
dependencies. Replace USERNAME
with your GitHub username and run the
following:
# Install bower globally if you don't have it:
npm install -g bower
# Replace USERNAME with your GitHub username:
git clone git@github.com:USERNAME/localForage.git
cd localForage
npm install
bower install
Omitting the bower dependencies will cause the tests to fail!
You need PhantomJS installed to run local tests. Run npm test
(or,
directly: grunt test
). Your code must also pass the
linter.
localForage is designed to run in the browser, so the tests explicitly require a browser environment. Local tests are run on a headless WebKit (using PhantomJS).
When you submit a pull request, tests will be run against all browsers that localForage supports on Travis CI using Sauce Labs.
As of version 1.7.3 the payload added to your app is rather small. Served using gzip compression, localForage will add less than 10k to your total bundle size:
This program is free software; it is distributed under an Apache License.
Copyright (c) 2013-2016 Mozilla (Contributors).
FAQs
Offline storage, improved.
We found that localforage demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.